package com.dearbinge.data.service.impl.im;

import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.im.UserContactService;
import com.dearbinge.data.dao.UserMapper;
import com.dearbinge.data.dao.im.ImUserContactMapper;
import com.dearbinge.data.dao.im.ImUserContactTagMapper;
import com.dearbinge.data.dao.im.ImUserContactTagRelationMapper;
import com.dearbinge.data.pojo.im.ImUserContact;
import com.dearbinge.data.pojo.im.ImUserContactTag;
import com.dearbinge.data.pojo.im.ImUserContactTagRelation;
import com.dearbinge.data.pojo.im.vo.ImUserContactVO;
import com.dearbinge.data.pojo.user.User;
import com.dearbinge.utils.Base64Util;
import com.dearbinge.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 好友列表业务实现层
 *
 * @author wes
 * @date 2019/2/27
 */
@Service(value = "userContactService")
public class UserContactServiceImpl implements UserContactService {

    @Autowired
    private ImUserContactMapper imUserContactMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ImUserContactTagRelationMapper imUserContactTagRelationMapper;
    @Autowired
    private ImUserContactTagMapper imUserContactTagMapper;

    /**
     * 删除
     *
     * @param id id
     * @return 结果
     */
    @Override
    public Integer delete(Integer id) {
        return imUserContactMapper.deleteByPrimaryKey(id);
    }

    /**
     * 更新
     *
     * @param userContact 好友列表
     * @return 结果
     */
    @Override
    public Integer update(ImUserContact userContact) {
        return imUserContactMapper.update(userContact);
    }

    /**
     * 插入
     *
     * @param userContact 好友列表
     * @return 结果
     */
    @Override
    public Integer insert(ImUserContact userContact) {
        //如果存在 则返回
        ImUserContact contact = imUserContactMapper.getContact(userContact.getUserId(), userContact.getFriendId());
        if (contact != null) {
            return 1;
        }
        //如果别名为空 默认插入用户名字
        if (userContact.getAlias() == null) {
            User user = userMapper.getById(userContact.getFriendId());

            userContact.setAlias(  Base64Util.decodeData(user.getName()));
            userContact.setAliasModified(false);
        } else {
            //别名不为空 插入别名
            userContact.setAliasModified(true);
        }
        return imUserContactMapper.insert(userContact);
    }

    /**
     * 根据id查询
     *
     * @param id id
     * @return 结果对象
     */
    @Override
    public ImUserContact getById(Integer id) {
        return imUserContactMapper.getByPrimaryKey(id);
    }

    /**
     * 根据用户ID查询好友列表 分页
     *
     * @param userId 用户id
     * @return 好友列表
     */
    @Override
    public Page listByUserId(Integer userId,Integer current, Integer size) {
        current = PageUtils.getDefaultValues(current, 1);
        size = PageUtils.getDefaultValues(size, 10);
        Page<ImUserContactVO> page = new Page<ImUserContactVO>(current, size);
       List<ImUserContactVO> userContact =  imUserContactMapper.listByUserId(userId,page);
       //插入标签集
        for (ImUserContactVO imUserContactVO : userContact) {
            User user = userMapper.getById(imUserContactVO.getFriendId());
            imUserContactVO.setFriendImg(user.getHeadImg());
            ArrayList<ImUserContactTag> list = new ArrayList<ImUserContactTag>();
            //根据关系id查询标签集
            List<ImUserContactTagRelation> listRelation = imUserContactTagRelationMapper.listByContactId(imUserContactVO.getId());
            for (ImUserContactTagRelation imUserContactTagRelation : listRelation) {
                list.add(imUserContactTagMapper.getByPrimaryKey(imUserContactTagRelation.getTagId()));
            }
         imUserContactVO.setTagList(list);
        }
        Integer count = imUserContactMapper.countByUserId(userId);
        page.setTotal(count);
        page.setRecords(userContact);
        return page;
    }

    @Override
    public String getIsHailFellow(String userId, String fellowId) {
        String id = imUserContactMapper.getIsHailFellow(userId,fellowId);
        return id;
    }


}
